import config from '@/config'
import {
	getCookie
} from '@/utils/base.js'
import store from '@/store'
class HTTP {
	constructor() {
		this.baseRestUrl = config.isProxy ? "" : config.baseUrl
		this.baseRestUrlWS = config.isProxy ? "" : config.baseUrlWS
		this.baseH5Url = config.isProxy ? "" : config.baseH5Url
	}

	request(params) {
		return new Promise((resolve, reject) => {
			// #ifdef MP-WEIXIN
			uni.showNavigationBarLoading()
			// #endif
			this._request(params, resolve, reject)
		})
	}

	//http 请求类, 当noRefech为true时，不做未授权重试机制
	_request({
		url,
		data = {},
		method = 'GET',
		type = ''
	}, resolve, reject) {
		var that = this;
		let api = type == 'ws' ? this.baseRestUrlWS : this.baseRestUrl;
		api += url;
		let header = {
			'content-type': 'application/json', // 默认值
			"Accept-Language": "en-US"
		}
		let Token = "";
		if (!url.includes(config.whiteList)) {
			Token = uni.getStorageSync('token');
			//Token='EAC921A15BDBFAD125E108816D475ED43D4F5B474F1F1069603DEB9828F9C65D085C6979339F0AEE579D841B45A811F72B44F34E8A6349F8165817B504421289356D370F9B64B1A36698DB7EC0531A92E57F7366F6366A835ABE9C85A6C100634D868FF2226E4E390005A34EB757F20878B7C2C0BB64585632C83C7037C6EF31D2969660B3418E0B4B8A201E7BB00FCF3C0F7617A8E5026BA8E669D2F1436F998CEB0C95F99D2227BFC622C7CD3F6C79E112B6CCEBDC187FD33EE7A66C3A6EE33C620250CB24534B6A4A742EDA81707886D53873C5B17377DF90B40AB1FA08B9C713F66ECBADDED28017ABBF7A9DC34FB82DE235E7C142FC'
			//Token = 'EAC921A15BDBFAD125E108816D475ED43D4F5B474F1F1069603DEB9828F9C65D085C6979339F0AEE579D841B45A811F72B44F34E8A6349F8165817B504421289356D370F9B64B1A36698DB7EC0531A92E57F7366F6366A835ABE9C85A6C100634D868FF2226E4E390005A34EB757F20878B7C2C0BB64585632C83C7037C6EF31D2969660B3418E0B4B8A201E7BB00FCF3C0F7617A8E5026BA8E669D2F1436F998CEB0C95F99D2227BFC622C7CD3F6C79590E3B76047EA8FA9F36F13069B9C9239FC340DC6F1B439BA2CE1A13149B1997255849FACBFBA07D6F78430C60544FB8AC18D6E5E84250A9CE6D74CEC86EAC5043629F0735C2720C'
			//Token = 'EAC921A15BDBFAD125E108816D475ED43D4F5B474F1F1069603DEB9828F9C65DBF89BC797DE3AD7D07B090448432DAF7A9AC07DA7465B7CF73EAD879726E7EB587BA8DDFE30340346FF9D921C5BAA9777F66B31B5D80B1F09B2C10996F9E482BF078D0E1C26F92230B51F892C11BC6B6D8DDDD7C5D45E8AA3CD61D010BCBF0D91A970C1E0DD5BE6F2C94F3A87DEC6FC24EC4D90F8A6907ECF7C584FB0D2B8961911BD1FE58AE282A88F78185E589D4BD51F457F8483E169AB023EAE7D8480D8C560CABD503C66AF7323064960CDF543BD4C8763E900E0D283FE4C24DF4CD3A96F19D185AAD987726546FACAD95F2131FDF202BAAB5D51A1A'
		}
		if (url.includes('/InternalAPI')) {
			api = this.baseH5Url + url;
			Token = uni.getStorageSync('h5Token');
		}
		uni.request({
			url: api,
			data: {
				Token,
				data
			},
			method,
			timeout: config.timeout,
			header,
			success(res) {
				// #ifdef MP-WEIXIN
				uni.hideNavigationBarLoading()
				// #endif
				var code = res.statusCode.toString();
				var startChar = code.charAt(0);
				if (startChar == '2') {
					if (res.data.ErrorCode === "0" || res.data.StatusCode == "0" || res.data.code == 200) {
						resolve(res.data)
					} else {
						if (res.data.ErrorCode * 1 === 1115) {
							console.log('登录过期')
							store.commit('setLogout')
						} else {
							uni.hideLoading()
							uni.showToast({
								title: res.data.Msg,
								icon: 'none',
								duration: 2000
							});
						}
						reject(res.data)
					}
				} else {
					uni.hideLoading()
					uni.showToast({
						title: '服务链接失败',
						icon: 'none',
						duration: 2000
					});
					reject(res)
				}
			},
			fail: function(err) {
				console.log('err', err)
				// #ifdef MP-WEIXIN
				uni.hideNavigationBarLoading()
				// #endif
				reject(err)
			}
		});
	}
};

export {
	HTTP
};


/* 
   /// <summary>
             /// 成功
             /// </summary>
             [Remark("成功")]
             Success = 0,
 
             /// <summary>
             /// UserToken为空
             /// </summary>
             [Remark("UserToken为空")]
             UserTokenNull = 100,
 
             /// <summary>
             /// UserToken错误
             /// </summary>
             [Remark("UserToken错误")]
             UserTokenError = 110,
 
             /// <summary>
             /// 请在微信浏览器打开
             /// </summary>
             [Remark("请使用微信打开")]
             NoWX = 120,
 
             /// <summary>
             /// 请关注公众号
             /// </summary>
             [Remark("请关注公众号")]
             NoSubscribe = 130,
 
             /// <summary>
             /// API用户不存在
             /// </summary>
             [Remark("API用户不存在")]
             ApiUserNull = 201,
 
             /// <summary>
             /// API用户被禁用
             /// </summary>
             [Remark("API用户被禁用")]
             ApiUserClose = 202,
 
             /// <summary>
             /// 签名错误
             /// </summary>
             [Remark("签名错误")]
             SignError = 203,
 
             /// <summary>
             /// 非认证IP
             /// </summary>
             [Remark("非认证IP")]
             IPError = 204,
 
             /// <summary>
             /// 请求已过期
             /// </summary>
             [Remark("请求已过期")]
             TimeError = 205,
 
             /// <summary>
             /// 系统错误
             /// </summary>
             [Remark("系统错误")]
             SystemError = 301,
 
             /// <summary>
             /// 商家信息错误
             /// </summary>
             [Remark("商家信息错误")]
             StoreError = 302,
 
             /// <summary>
             /// 请登录
             /// </summary>
             [Remark("请登录")]
             NoLogin = 610,
 
             /// <summary>
             /// 请注册
             /// </summary>
             [Remark("请注册")]
             NoRegister = 620,
 
             /// <summary>
             /// 账号被禁用
             /// </summary>
             [Remark("账号被禁用")]
             Forbidden = 630,
 
             /// <summary>
             /// 其他错误
             /// </summary>
             [Remark("其他错误")]
             OtherError = 900,
 
             /// <summary>
             /// 网络异常
             /// </summary>
             [Remark("网络异常")]
             NetworkError = 901
 
 */